package x.y.z;

import java.util.ArrayList;

public class Interview_4_5_ValidateBST {
    private ArrayList<Integer> list = new ArrayList<>();

    public boolean isValidBST(TreeNode root) {
        if (root == null) {
            return true;
        }
        getElement(root);
        for (int i = 1; i < list.size(); i++) {
            if (list.get(i) <= list.get(i - 1)) {
                return false;
            }
        }
        return true;
    }

    private void getElement(TreeNode root) {
        if (root.left != null) {
            getElement(root.left);
        }
        list.add(root.val);
        if (root.right != null) {
            getElement(root.right);
        }
    }
}